package com.wofish.goods.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import com.wofish.common.core.text.Convert;
import com.wofish.common.json.JsonUtil;
import com.wofish.common.utils.StringUtils;
import com.wofish.goods.domain.SpecValues;
import com.wofish.goods.domain.Specification;
import com.wofish.goods.mapper.SpecValuesMapper;
import com.wofish.goods.mapper.SpecificationMapper;
import com.wofish.goods.service.ISpecValuesService;
import com.wofish.goods.service.ISpecificationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * 商品规格 服务层实现
 * 
 * @author wofish
 * @date 2019-04-13
 */
@Slf4j
@Service
public class SpecificationServiceImpl implements ISpecificationService 
{
	@Autowired
	private SpecificationMapper specificationMapper;

	@Autowired
	private SpecValuesMapper specValuesMapper;

	/**
     * 查询商品规格信息
     * 
     * @param specId 商品规格ID
     * @return 商品规格信息
     */
    @Override
	public Specification selectSpecificationById(Long specId)
	{
	    return specificationMapper.selectSpecificationById(specId);
	}
	
	/**
     * 查询商品规格列表
     * 
     * @param specification 商品规格信息
     * @return 商品规格集合
     */
	@Override
	public List<Specification> selectSpecificationList(Specification specification)
	{
	    return specificationMapper.selectSpecificationList(specification);
	}
	
    /**
     * 新增商品规格
     * 
     * @param specification 商品规格信息
     * @return 结果
     */
	@Override
	public int insertSpecification(Specification specification) {

		return specificationMapper.insertSpecification(specification);
	}


	@Transactional
	@Override
	public int insertSpecificationAndValue(Specification specification, String specValuesJson){
		Date date = new Date();
		specification.setCreateTime(date);
		specification.setUpdateTime(date);
		int row = insertSpecification(specification);
		log.info("insertSpecificationAndValue row:{}",row);
		batchSaveSpecValues(specValuesJson,specification.getSpecId());

		return row;

	}

	private void batchSaveSpecValues(String specValuesJson, Long specId){
		/** 批量保存规格值 */
		if (StringUtils.isNotBlank(specValuesJson)) {
			List<SpecValues> list = JsonUtil.strToObj(specValuesJson, new TypeReference<List<SpecValues>>() {
			});
			list.stream().forEach(bean -> bean.setSpecId(specId));
			specValuesMapper.insertBatchSpecValues(list);
		}

	}

	/**
     * 修改商品规格
     * 
     * @param specification 商品规格信息
     * @return 结果
     */
	@Override
	public int updateSpecification(Specification specification, String specValuesJson)
	{
		//先删除之前的规格值，再批量保存
		specValuesMapper.deleteSpecValuesBySpecId(specification.getSpecId());
		batchSaveSpecValues(specValuesJson,specification.getSpecId());

	    return specificationMapper.updateSpecification(specification);
	}

	/**
     * 删除商品规格对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteSpecificationByIds(String ids)
	{
		return specificationMapper.deleteSpecificationByIds(Convert.toStrArray(ids));
	}

	public List<Specification> querySpecByTypeId(Long typeId){
		return specificationMapper.querySpecByTypeId(typeId);
	}

	
}
